using System;
using System.Web;
using SoftwareMonkeys.Scriptech.Diagnostics;

namespace SoftwareMonkeys.Scriptech.Scripts
{
	public class Scripts
	{
		static public string CurrentScript
		{
			get
			{
				return GetCurrentScript(true);
			}
		}
		
		static public string GetCurrentScript(bool includeQueryStrings)
		{
			string script = String.Empty;
			
			using (LogGroup logGroup = LogGroup.StartDebug("Retrieving current script name."))
			{
				LogWriter.Debug("Include query strings: " + includeQueryStrings);
				
				if (HttpContext.Current.Request.QueryString["Script"] != null
				    && HttpContext.Current.Request.QueryString["Script"] != String.Empty)
				{
					LogWriter.Debug("Retrieving from query string.");
					
					script = HttpContext.Current.Request.QueryString["Script"];
					
					LogWriter.Debug("Script (basic): " + script);
					
					if (includeQueryStrings)
						script = AddOriginalQueryString(script);
					
					LogWriter.Debug("Script (with query strings): " + script);
				}
				else
				{
					LogWriter.Debug("Retrieving from URL.");
					
					string url = HttpContext.Current.Request.Url.ToString ();
					
					LogWriter.Debug("URL: " + url);
								
					script = "Not found";
					
					string basePath = new UrlMapper().MapUrl("/Scripts");
					
					LogWriter.Debug("Base path: " + basePath);
					
					if (url.IndexOf (basePath) > -1)
						script = url.Replace (basePath, "").Trim ('/');
					
					LogWriter.Debug("Script (after base path removed): " + script);
					
					if (!includeQueryStrings && script.IndexOf("?") > -1)
						script = script.Substring(0, script.IndexOf("?"));
										
					LogWriter.Debug("Script (after query string removed): " + script);
					
				}
				
				LogWriter.Debug("Script: " + script);
			}
			
			return script;
		}
		
		static internal string AddOriginalQueryString(string scriptName)
		{
			string scriptWithQueryString = String.Empty;
			
			using (LogGroup logGroup = LogGroup.StartDebug("Adding original query string to script name: " + scriptName))
			{
				string originalQueryString = HttpContext.Current.Request.Url.Query;
				
				LogWriter.Debug("Orignal query string: " + originalQueryString);
				
				string fixedQueryString = originalQueryString.Replace("Script=" + scriptName, String.Empty);
				
				LogWriter.Debug("Fixed query string #1:" + fixedQueryString);
				
				fixedQueryString = fixedQueryString.Trim('?');
				
				LogWriter.Debug("Fixed query string #2:" + fixedQueryString);
				
				fixedQueryString = fixedQueryString.Trim('&').Trim('&');
				
				LogWriter.Debug("Fixed query string #3:" + fixedQueryString);
				
				scriptWithQueryString = scriptName + "?" + fixedQueryString;
				
				scriptWithQueryString = scriptWithQueryString.Trim('?');
				
				LogWriter.Debug("Script with query string: " + scriptWithQueryString);
			}
			return scriptWithQueryString;
		}
		
		static public string MapUrl(string scriptName)
		{
			return new UrlMapper().MapUrl(scriptName);
		}
		
		static public string MapUrl(string scriptName, string queryStrings)
		{
			return new UrlMapper().MapUrl(scriptName, queryStrings);
		}
		
		
	}
}

